package com.epam.tcs.actions.pages;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.tcs.actions.BreadCrumb;
import com.epam.tcs.dao.factory.DAO;
import com.epam.tcs.dao.hibernate.entities.Group;
import com.epam.tcs.dao.hibernate.entities.Program;
import com.epam.tcs.dao.hibernate.entities.Theme;

public class LearningPage extends PageAbstract{

	@Override
	public void doExec(HttpServletRequest req, HttpServletResponse res) {

//		String target = req.getRequestURI().substring(
//				req.getContextPath().length());
//
//		String[] tarr = target.split("/");
		
		BreadCrumb.add(req, res, "Учебный процесс", "/tcs/learning");
		
		try {
			showLearning(req, res);
		} catch (ServletException e) {
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
	}
	
	private void showLearning(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		DAO<Program> program = dao.getProgramDAO();
		DAO<Theme> theme = dao.getThemeDAO();
		DAO<Group> group = dao.getGroupDAO();
		if (req.getMethod() == "POST") {
			if (req.getParameter("selected_program") != null
					&& req.getParameter("selected_theme") != null
					&& req.getParameter("selected_group") != null) {

				req.getSession().setAttribute(
						"current_program",
						program.getElement(Long.parseLong(req
								.getParameter("selected_program"))));
				req.getSession().setAttribute(
						"current_theme",
						theme.getElement(Long.parseLong(req
								.getParameter("selected_theme"))));
				req.getSession().setAttribute(
						"current_group",
						group.getElement(Long.parseLong(req
								.getParameter("selected_group"))));
			}
		} else {
			req.getSession().setAttribute("listThemes", theme.getAllElements());
			req.getSession().setAttribute("listGroups", group.getAllElements());
			req.getSession().setAttribute("listPrograms",
					program.getAllElements());
		}

		outputPage("learning/learning.jsp", req, res);
	}


}
